package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.OperatorDTO;
import com.woniu.pojo.Operator;
import com.woniu.service.IOperatorService;
import com.woniu.utils.MobileUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sss
 * @since 2023-01-03
 */
@RestController
@RequestMapping("/operator")
@Api(tags = "运维人员")
public class OperatorController {
    @Autowired
    private IOperatorService operatorService;

    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result listSearch(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody(required = false) OperatorDTO operatorDTO) {
        PageData pageData = operatorService.listSearch(currentPage,pageSize,operatorDTO);
        return new Result(200,"查询成功",pageData);
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody Operator operator) {
        String phone = operator.getPhone();
        boolean b = MobileUtil.checkPhone(phone);
        System.out.println("b="+b);
        if (b) {
            int result = operatorService.edit(operator);
            if (result > 0) {
                return new Result(200, "编辑成功");
            }
            return new Result(500, "修改失败");
        }
        return new Result(500,"手机号码格式错误");
    }

    @PostMapping("/save")
    public Result save(@RequestBody Operator operator) {
        boolean b = MobileUtil.checkPhone(operator.getPhone());
        if (b) {
            boolean save = operatorService.save(operator);
            if (save) {
                return new Result(200, "保存成功");
            }
            return new Result(500, "保存失败");
        }
        return new Result(500,"手机号码格式错误");
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        Operator operator = operatorService.getById(id);
        return new Result(200,"查询成功",operator);
    }

    @PutMapping("/forbidden/{id}")
    public Result forbiddenById(@PathVariable String id) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        Operator operator = operatorService.getById(id);
        operator.setStatus("1");
        boolean update = operatorService.update(operator, wrapper);
        if (update) {
            return new Result(200, "禁用成功");
        }
        return new Result(500,"禁用失败");

    }

    @PutMapping("/recover/{id}")
    public Result recoverById(@PathVariable String id) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        Operator operator = operatorService.getById(id);
        operator.setStatus("0");
        boolean update = operatorService.update(operator, wrapper);
        if (update) {
            return new Result(200, "恢复成功");
        }
        return new Result(500,"恢复失败");

    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {

        boolean b = operatorService.removeById(id);
        if (b) {
            return new Result(200, "删除成功");
        }
        return new Result(500,"删除失败");
    }

}
